package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FCJ
 * @date 2020-01-02 11:44
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    //根据条件，查询分页
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> page = checkGroupDao.selectByCondition(queryString);
        List<CheckItem> rows = page.getResult();
        long total = page.getTotal();
        return new PageResult(total, rows);
    }

    //添加检查组
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkItemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkGroupId", checkGroupId);
                map.put("checkItemId", checkItemId);
                checkGroupDao.addrelation(map);
            }
        }
    }

    //根据id查找检查组
    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);
        return checkGroup;
    }

    //根据id查询完整检查组（包含该组所行检查项信息）
    @Override
    public CheckGroup findOne(Integer id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);
        List<CheckItem> list = checkGroupDao.findCheckItemByCheckGroupId(id);
        checkGroup.setCheckItems(list);
        return checkGroup;
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.edit(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        checkGroupDao.deleterelation(checkGroupId);

        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkItemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkGroupId", checkGroupId);
                map.put("checkItemId", checkItemId);
                checkGroupDao.addrelation(map);
            }
        }
    }

    @Override
    public void deleteById(Integer id) {
        checkGroupDao.deleteById(id);
    }

    @Override
    public void deleteRelation(Integer id) {
        checkGroupDao.deleterelation(id);
    }

    @Override
    public List<CheckGroup> findAllCheckGroup() {
        return checkGroupDao.findAllCheckGroup();
    }


}
